package com.zxjbyte.yiyi.server.annotation;

import com.zxjbyte.yiyi.framework.common.enums.ExtensionBundles;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Arrays;
import java.util.Map;

/**
 * @Author zhangxingjia
 * @Date 2025/3/19 18:10
 * @Version: 1.0
 */
@Slf4j
public class ExtensionBundlesImportSelector implements ImportSelector, EnvironmentAware {


    private Environment environment;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }


    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        // 读取 @EnableFeature 注解上的值
        Map<String, Object> attributes = importingClassMetadata.getAnnotationAttributes(EnableExtensionBundles.class.getName());
        if (attributes == null) {
            return new String[0];
        }
        ExtensionBundles[] bundles = (ExtensionBundles[]) attributes.get("value");
        if (bundles == null || bundles.length == 0) {
            return new String[0];
        }
        return Arrays.stream(bundles).map(ExtensionBundles::getConfigClassNames).filter(classes -> classes != null && classes.length > 0).flatMap(Arrays::stream).toArray(String[]::new);
    }
}
